<template>
  <div class="app">
    <el-card style="width: 480px">
      <template #header>
        <div class="card-header">
          <h2>登录</h2>
        </div>
      </template>

      <!-- 登录表单 -->
      <!--  :rules="rules" 整个表单的校验规则 -->
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <!-- prop="name" 采用的name属性的校验规则-->
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"> 登录 </el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>

      <template #footer>Footer content</template>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
let router = useRouter()
import { ElMessage } from 'element-plus'
import { loginRequest } from '@/api/manager'

const formSize = ref('default')
// 组件实例
const ruleFormRef = ref()
// 响应式数据
const ruleForm = reactive({
  name: '',
  password: '',
})

//整个表单的校验规则
//FormRule 限制的是验证规则可以有哪些属性
const rules = reactive({
  // name 属性的规则
  password: [
    {
      pattern: /^.{5,}$/,
      message: '密码至少5位非空字符',
      required: true,
    },
  ],
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 35, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
})

// formEl form 表单的组件实例
const submitForm = async (formEl) => {
  if (!formEl) return

  //validate对整个表单的内容进行验证。接收一个回调函数，或返回Promise
  await formEl.validate(async (valid, fields) => {
    // valid 是否校验通过
    if (valid) {
      console.log('submit!')
      console.log(ruleForm.name)
      console.log(ruleForm.password)

      //校验通过调登录接口 --登录成功进行跳转
      const r = await loginRequest(ruleForm.name, ruleForm.password)
      console.log(r.data)
      const data = r.data

      if (data.code == '1') {
        ElMessage({
          message: data.message,
          type: 'success',
          plain: true,
        })
        //登录成功存入token和adminname到本地
        localStorage.setItem('manager-data', data.data)
        router.push('/')
      } else {
        ElMessage({
          message: data.message,
          type: 'error',
          plain: true,
        })
      }
    } else {
      //fields 校验不通过
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>

<style lang="css" scoped>
.app {
  background-image: url(../images/image.png);
  height: 100vh;
  display: flex;
}
.el-card {
  margin: auto;
}
</style>
